﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TSharp.Core.Authentication
{
    #region 注释
    /* ====================
     * 作者：wangyao
     * 创建时间：2013/6/3 15:50:19
     * ====================
     */
    #endregion
    /// <summary>
    /// 登陆用户Session管理类
    /// </summary>
    public class LoginUserSessionManager
    {
        /// <summary>
        /// 登录用户Session的Key
        /// </summary>
        private static readonly string SESSION_KEY_LOGIN_USER = "SESSION_KEY_LOGIN_USER";

        /// <summary>
        /// 将登录用户添加到Session中
        /// </summary>
        public static void SetLoginUser(ILoginUser loginUser)
        {
            AopContext.GetContext().Session[SESSION_KEY_LOGIN_USER] = loginUser;
        }

        /// <summary>
        /// 获取当前登录用户Session
        /// </summary>
        public static ILoginUser GetLoginUser()
        {
            var loginUser = (ILoginUser)AopContext.GetContext().Session.GetOrAdd(SESSION_KEY_LOGIN_USER, key => AopContext.GetDefaultLactor().Get<ILoginService>().GetLoginUser());
            return loginUser;
        }

        /// <summary>
        /// 将登录用户从Session中移除
        /// </summary>
        public static void DeleteLoginUserSession()
        {
            AopContext.GetContext().Session.Remove(SESSION_KEY_LOGIN_USER);
        }
    }
}
